package com.foreknow.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foreknow.config.CommonPage;
import com.foreknow.entity.Rule;
import com.foreknow.entity.Scheduling;
import com.foreknow.service.SchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Api(tags = "SchedulingController",description = "排班计划管理")
@RestController
@RequestMapping("/scheduling")
public class SchedulingController {

    @Autowired
    private SchedulingService schedulingService;

    @ApiOperation("查询排班计划")
    @PostMapping("/getScheduling")
    public List<Object> getScheduling(Integer deptCategoryId, Integer deptId, Integer registLeId,String startDate, String endDate){
        List<Object> list = schedulingService.getScheduling(deptCategoryId,deptId,registLeId,startDate,endDate);
        return list;
    }

    @ApiOperation("新增排班计划")
    @PostMapping("/addScheduling")
    public Boolean addScheduling(@RequestBody Map<String,Object> para){
        List<Rule> list = JSON.parseArray(JSON.toJSONString(para.get("ruleList")),Rule.class);
        LocalDate startDate=LocalDate.parse((String)para.get("startDate"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate endDate=LocalDate.parse((String)para.get("endDate"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return schedulingService.addScheduling(list,startDate,endDate);
    }

    @ApiOperation("修改排班计划")
    @PostMapping("/updateScheduling")
    public Boolean updateScheduling(@RequestBody String schedulingList){
        JSONObject jsonObject = JSONObject.parseObject(schedulingList);
        List<Scheduling> list = JSONObject.parseArray(jsonObject.getJSONArray("schedulingList").toJSONString(), Scheduling.class);
        return schedulingService.updateScheduling(list);
    }
}
